<?php
/**
 * The controller to manage information of medicine.
 *
 */
class C_Country extends Controller {
	
	public $template_mod;
	
	function C_Country($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'Country') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'admin';
		}
		$this->template_mod = $template_mod;				
		$this->view->path = 'country';
		$this->setLayout('admin');
	}
	
	public function actionManagement() {
		return $this->view->render('country_management.html');
	}
	
	public function actionListCountry() {
		$countryList = Celini::newORDO("Country") -> getCountryList();
		$this->assign('countryList', $countryList);
		
		return $this->view->render('list.html');
		
	}
	
	public function actionEditCountry() {
		$id = $_GET['service_id'];
		$country = Celini::newORDO('Country');
		
		if (isset($id) && $id != "") {
			$country->setup($id);
		}
		
		$this->assign_by_ref('country', $country);
		return $this->view->render('edit_country.html');
		
	}
	
	public function actionSaveCountryAjax() {
		$field		=	L10nHelper::getLocale();	
		$field 		= 	L10nHelper::changeField($field,"name");
		$country = Celini::newORDO('Country');

		if (isset($_POST['id']) && $_POST['id'] != 0) {
			//edit
			$data = array($field => Utils::sanitizeText($_POST['name']));
            $country->setup($_POST['id']);
       	} else {
       		//addnew to all supported languages
       		$locales = L10nHelper::getSupportedLocales();
       		$data = array();
       		foreach ( $locales as $lo => $la ) {
       			$lo 		= 	L10nHelper::changeField($lo,"name");
       			$data[$lo] = Utils::sanitizeText($_POST['name']);
       		}      		
       	}	
       	$country->populateArray($data);            
        $country->persist();     
		return $country->get_id();
	}
	
	public function actionChangeCountryStatusAjax() {
		$id = $_GET['id'];
		$status = $_GET['status'];
		$country = Celini::newORDO('Country', $id);
		if($status == 'lock'){
			$country->disableCountry($id);
		}
		else if($status == 'unlock'){
			$country->enableCountry($id);
		}
		
	}
 
	
}
?>